<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../res/layui/css/layui.css">
    <style>
        #userInfoDialog {
            padding: 20px;
            width: 400px;
            display: none; /*隐藏层*/
        }

        #userInfoDialog p {
            padding: 10px 0;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">管理员列表</div>
        <div class="layui-card-body">
            <table id="adminlist" lay-filter="adminFilter"></table>
        </div>
    </div>
</div>
<!--用户信息对话框-->
<div id="userInfoDialog">
    <input type="hidden" id="userid">
    <table class="layui-table">
        <tr>
            <td>用户名</td>
            <td><span id="username"></span></td>
        </tr>
        <tr>
            <td>邮箱</td>
            <td><span id="email"></span></td>
        </tr>
        <tr>
            <td>手机号</td>
            <td><span id="phone"></span></td>
        </tr>
        <tr>
            <td>添加时间</td>
            <td><span id="addtime"></span></td>
        </tr>
        <tr>
            <td>状态</td>
            <td><span id="status"></span></td>
        </tr>
    </table>
</div>

<script src="../res/layui/layui.js"></script>
<script>
    layui.use(['table', 'layer', 'jquery', 'util'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        var util = layui.util;

        // 渲染表格
        table.render({
            elem: '#adminlist',
            url: '/admin/list',
            size: 'sm',
            page: {
                limit: 2,
                limits: [2, 5, 10, 15, 20]
            },
            cols: [[
                {type: 'checkbox'},
                {title: 'uid', type: 'numbers'},
                {title: '用户名', field: 'username'},
                {title: '密码', field: 'password'},
                {title: '手机号', field: 'phone'},
                {
                    title: '注册时间',
                    templet: '<div>{{layui.util.toDateString(d.regtime, "yyyy年MM月dd日 HH:mm:ss")}}</div>'
                },
                {title: '头像', templet: '<div><img src="{{d.img}}" alt=""></div>'},
                {
                    title: '生日',
                    templet: '<div>{{layui.util.toDateString(d.birth, "yyyy年MM月dd日 HH:mm:ss")}}</div>'
                },
                {title: '状态', templet: function (d) {
                        if (d.status === 0) {
                            return '<span class="layui-badge layui-bg-red">禁用</span>'
                        } else if (d.status === 1) {
                            return '<span class="layui-badge layui-bg-green">正常</span>'
                        }
                    }},
                {title: '盐值', field: 'salt'},
                {title: '操作', templet: '#rowTools'} // 自定义行工具栏
            ]],
            toolbar: '#toolbar', //自定义头工具栏
        });

        // 监听工具条
        table.on('tool(adminFilter)', function (obj) {
            var data = obj.data; // 获得当前行数据
            var layEvent = obj.event; // 获得 lay-event 对应的值

            if (layEvent === 'details') {
                // 显示详情信息
                $('#userid').val(data.id); // 设置隐藏字段的值
                $('#username').text(data.username);
                $('#email').text(data.email);
                $('#phone').text(data.phone);
                $('#addtime').text(util.toDateString(data.regtime, "yyyy年MM月dd日 HH:mm:ss"));
                $('#status').text(data.status === 0 ? '禁用' : '正常');
                layer.open({
                    type: 1,
                    title: '用户详情',
                    area: ['400px', '300px'],
                    content: $('#userInfoDialog')
                });
            } else if (layEvent === 'del') {
                // 删除用户
                layer.confirm('确定删除该用户吗？', function (index) {
                    $.ajax({
                        url: '/admin/delete',
                        type: 'POST',
                        data: { id: data.id },
                        success: function (res) {
                            if (res.success) {
                                layer.msg('删除成功');
                                table.reload('adminlist');
                            } else {
                                layer.msg('删除失败');
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>

<!--操作栏自定义模板-->
<script id="rowTools" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="details">
            <i class="layui-icon layui-icon-list"></i>
            详情
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
        {{# if(d.status === 0){ }}
        <button class="layui-btn layui-btn-warm layui-btn-xs" lay-event="updateStatus" data-status="1">
            <i class="layui-icon layui-icon-close-fill"></i>
            禁用
        </button>
        {{# }else{ }}
        <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="updateStatus" data-status="0">
            <i class="layui-icon layui-icon-ok-circle"></i>
            启用
        </button>
        {{# } }}
    </div>
</script>

</body>
</html>
